<template>
  <div>
    <h1>物品列表</h1>
    <el-table :data="items.slice((currpage - 1) * pagesize, currpage * pagesize)">
      <el-table-column prop="_id" label="Id" width="260"></el-table-column>
      <el-table-column prop="name" label="英雄名称"></el-table-column>
      <el-table-column prop="title" label="称号"></el-table-column>
      <el-table-column prop="avater" label="头像">
        <template slot-scope="scope">
          <img :src="scope.row.avater" alt style="display:block;height:3rem" />
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="100">
        <template slot-scope="scope">
          <!-- <template> -->
          <el-button
            @click="$router.push(`/heroes/edit/${scope.row._id}`)"
            type="text"
            size="small"
          >编辑</el-button>
          <el-button @click="remove(scope.row)" type="text" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--  -->
    <el-pagination
      background
      layout="prev, pager, next, sizes, total, jumper"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="pagesize"
      :total="items.length"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    ></el-pagination>
    <!--  -->
  </div>
</template>

<script>
export default {
  name: "",
  data() {
    return {
      items: [],
      msg: 8888,
      pagesize: 10,
      currpage: 1
    };
  },
  created() {
    this.fetch();
  },
  methods: {
    fetch() {
      this.$http.get("/rest/heroes").then(res => {
        this.items = res.data;
        console.log(this.items);
      });
    },
    handleCurrentChange(cpage) {
      this.currpage = cpage;
    },
    handleSizeChange(psize) {
      this.pagesize = psize;
    },
    handleSelectionChange(val) {
      console.log(val);
    },

    // fetch() {
    //   this.$http.get("/rest/heroes").then(res => {
    //     this.items = res.data;
    //     console.log(this.items);
    //   });
    // },
    remove(row) {
      this.$confirm(`是否删除分类"${row.name}"`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(async () => {
        //     console.log(row._id)
        //   this.$http.delete(`/cotegories/${row._id}`).then(res =>{
        //       console.log(res)
        //   })
        const res = await this.$http.delete(`/rest/heroes/${row._id}`);
        this.fetch();
        console.log(res);
        this.$message({
          type: "success",
          message: "删除成功!"
        });
      });
      // .catch(() => {});
    }
  }
};
</script>

<style lang='css' scoped>
</style>